package com.tanhua.manage.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.tanhua.manage.domain.Admin;
import com.tanhua.manage.interceptor.AdminHolder;
import com.tanhua.manage.service.AdminService;
import com.tanhua.manage.vo.AdminVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/system/users")
@Slf4j
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 后台登陆时 图片验证码 生成
     */
    @GetMapping("/verification")
    public void showValidateCodePic(String uuid, HttpServletRequest req, HttpServletResponse res) {
        log.debug("后台图片验证码生成~");
        res.setDateHeader("Expires", 0);
        res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        res.addHeader("Cache-Control", "post-check=0, pre-check=0");
        // Set standard HTTP/1.0 no-cache header.
        res.setHeader("Pragma", "no-cache");
        res.setContentType("image/jpeg");
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(299, 97);
        String code = lineCaptcha.getCode();
        log.debug("uuid={},code={}", uuid, code);
        adminService.saveCode(uuid, code);
        try {
            lineCaptcha.write(res.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ResponseEntity AdminLogin(@RequestBody Map map) {
        log.debug("后台用户登录请求~");
        //调用service层 传入map参数 返回token
        String token = adminService.AdminLogin(map);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token",token);
        //返回状态信息
        return ResponseEntity.ok(hashMap);
    }

    /**
     * 用户基本信息
     */
    @PostMapping("/profile")
    public ResponseEntity queryUserData() {
        //获取当前用户的id
        Long currentUserId = AdminHolder.getUserId();
        //根据用户Id 查询admin表 返回admin对象
        Admin admin = adminService.query().eq("id", currentUserId).one();
        //将对象封装成vo 返回
        AdminVo adminVo = new AdminVo();
        BeanUtils.copyProperties(admin,adminVo);
        //返回状态信息
        return ResponseEntity.ok(adminVo);
    }

    /**
     * 用户登出
     */
    @PostMapping("/logout")
    public ResponseEntity logout(@RequestHeader("Authorization")String authorizationData) {
        log.debug("正在退出~");
        //将Token参数进行去除前缀
        String token= authorizationData.replace("Bearer ", "");
        //调用adminService方法 传入token参数
        return adminService.logout(token);

    }


}